package wallegame.gui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

public class MainApplet extends JApplet {
	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;
	private NavigationPanel navigationPanel;
	private InstructionPanel instructionsPanel;
	private InfoPanel infoPanel;
	private GUIController guiController;
	private boolean end = false;
	private MainWindow mainWindow;
	private JPanel mainPanel;

	public MainApplet() {
		guiController = guiController;
	}

	// Called when this applet is loaded into the browser.
	public void init() {
		
		resize(900, 700);
		noDefaultMenu();

		try {
			SwingUtilities.invokeAndWait(new Runnable() {

				public void run() {

					mainPanel = new JPanel(new BorderLayout());
					JLabel lbl = new JLabel("Walle Game");
					RobotPanel robotPanel = new RobotPanel();
					navigationPanel = new NavigationPanel();
					infoPanel = new InfoPanel();
					createMenu();

					instructionsPanel = new InstructionPanel(guiController,
							robotPanel, mainWindow);

					JSplitPane SuperiorPanel = new JSplitPane(
							JSplitPane.HORIZONTAL_SPLIT, instructionsPanel,
							robotPanel);
					JSplitPane inferiorPanel = new JSplitPane(
							JSplitPane.VERTICAL_SPLIT, SuperiorPanel,
							navigationPanel);

					mainPanel.add(lbl);
					mainPanel.add(SuperiorPanel, BorderLayout.NORTH);
					mainPanel.add(inferiorPanel, BorderLayout.CENTER);
					mainPanel.add(infoPanel, BorderLayout.SOUTH);

					add(mainPanel);

				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't complete successfully");
		}
	}

	private void noDefaultMenu() {
		Frame[] frames = Frame.getFrames();
		for (Frame frame : frames) {
			frame.setMenuBar(null);
			frame.pack();
		}
	}

	public void createMenu() {
		JMenuBar mbMenu = new JMenuBar();
		this.setJMenuBar(mbMenu);
		JMenu mDialogs = new JMenu("File");
		mbMenu.add(mDialogs);
		initializeMenu(mDialogs);
	}

	private void initializeMenu(JMenu mDialogs) {

		JMenuItem mQuit = new JMenuItem("Quit");
		mDialogs.add(mQuit);
		mQuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, "Game Over");
				System.exit(0);

			}
		});
	}

}